/**
 * 
 */
package de.nak.fireDepartment.administrationAction;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import org.springframework.web.struts.ActionSupport;

import de.nak.fireDepartment.actionForms.CarForm;
import de.nak.fireDepartment.exceptions.CarAlreadyExistsException;
import de.nak.fireDepartment.exceptions.ImageUploadException;
import de.nak.fireDepartment.model.Car;
import de.nak.fireDepartment.service.CarService;
import de.nak.fireDepartment.service.ImageUploadService;
import de.nak.fireDepartment.service.LoginService;

/**
 * Action class for creating and edit a car.
 * @author Nils Bredow
 */
public class CreateEditCarAction extends ActionSupport {
	
	/**
	 * This method creates or updates a car
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return actionforward
	 * @throws Exception
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// ActionForm => CarForm		
		CarForm carForm = (CarForm) form;
		// Get the car from the carform
		Car car = carForm.getCar();
		// Clear ErrorCode to display
		carForm.setErrorCode("");
		
		// get the car and image Services
		CarService carService = (CarService)getWebApplicationContext().getBean("carService");
		ImageUploadService imageUploadService = (ImageUploadService)getWebApplicationContext().getBean("imageUploadService");
		
		// Validate the user-login
		LoginService loginService = (LoginService)getWebApplicationContext().getBean("loginService");
		if(!loginService.checkUserIsValidated()){
			return mapping.findForward("missinglogin");
		}
		
		// get the formfile
		FormFile aFile = carForm.getaCarImageFile();
        String filePath = "";
        // if there are errors while uploading, the message should be put into the errorcode
        try{
        	filePath = imageUploadService.uploadFile(aFile, ImageUploadService.CAR, getServletContext());
        }catch(ImageUploadException ex){
        	carForm.setErrorCode(ex.getMessage());
        }
        
        // if the car id is set, you have to update the car, otherwise create a new car.
        try{
        	if(car.getCarId() > 0){
        		carService.updateCar(car.getCarId(), car.getModelName(), car.getYearOfManufacture(), car.getEnginePerformance(), car.getEngineSize(), car.getManufactureCompany(), car.getMeasurementsLength(), car.getMeasurementsHigh(), car.getMeasurementsWidth(), car.getCrew(), car.getDescriptionText(), filePath, carForm.getSelectedEquipmentIds());
    		}else{
    			carService.createCar(car.getModelName(), car.getYearOfManufacture(), car.getEnginePerformance(), car.getEngineSize(), car.getManufactureCompany(), car.getMeasurementsLength(), car.getMeasurementsHigh(), car.getMeasurementsWidth(), car.getCrew(), car.getDescriptionText(), filePath, carForm.getSelectedEquipmentIds());
    		}
        }catch(CarAlreadyExistsException ex){
        	carForm.setErrorCode(ex.getMessage());
        }
        	
		// Reload list
		carForm.setCars(carService.listCars());
		// Return result
		return mapping.findForward("success");
	}
}
